/**
 * 
 */
package com.omni.web.controllers;

import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.omni.web.utils.CaptchaGenerator;
import com.omni.web.utils.ImageUtil;

/**
 * @author timdong
 * 
 */

@Controller
@RequestMapping("/user")
public class UserController extends BaseController  {
  
	private final CaptchaGenerator generator;
	
	/** 
	 * <p>Title: </p> 
	 * <p>Description: </p>  
	 */
	public UserController() {
		
		generator = new CaptchaGenerator();
		generator.setCodeLength(4);
		generator.setHeight(25);
		generator.setWidth(60);
	}
	
	@RequestMapping(value = "/index", method=RequestMethod.GET)
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
		
		return new ModelAndView("user/index");
	}
	
	@RequestMapping(value = "/add", method=RequestMethod.POST)
	public ModelAndView add(HttpServletRequest request, HttpServletResponse response) {
		
		return new ModelAndView("user/index");
	}
	
	@RequestMapping(value = "/captcha", method=RequestMethod.GET)
	public void generateImage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		 response.setHeader("Pragma", "No-cache");    
         response.setHeader("Cache-Control", "No-cache");    
         response.setDateHeader("Expires", 0);    
         response.setContentType("image/jpeg");    
     
         String code = generator.generateRandomMixedCode();    
    
         HttpSession session = request.getSession(true);    
         session.setAttribute("CaptchaCode", code);    
         ImageIO.write(generator.getImage(code), "JPEG", response.getOutputStream());
	}
	
	@RequestMapping(value = "/uploadAvatar")
    public String uploadHeadImage(
            HttpServletRequest request,
            @RequestParam(value = "x") String x,
            @RequestParam(value = "y") String y,
            @RequestParam(value = "h") String h,
            @RequestParam(value = "w") String w,
            @RequestParam(value = "imgFile") MultipartFile imageFile
    ) throws Exception{
        String realPath = request.getSession().getServletContext().getRealPath("/");
        String resourcePath = "upload/";
        if(imageFile!=null){
//            if(FileUploadUtil.allowUpload(imageFile.getContentType())){
//                String fileName = FileUploadUtil.rename(imageFile.getOriginalFilename());
//                int end = fileName.lastIndexOf(".");
//                String saveName = fileName.substring(0,end);
//                File dir = new File(realPath + resourcePath);
//                if(!dir.exists()){
//                    dir.mkdirs();
//                }
//                File file = new File(dir,saveName+"_src.jpg");
//                imageFile.transferTo(file);
//                String srcImagePath = realPath + resourcePath + saveName;
//                int imageX = Integer.parseInt(x);
//                int imageY = Integer.parseInt(y);
//                int imageH = Integer.parseInt(h);
//                int imageW = Integer.parseInt(w);
//                
//                ImageUtil.imgCut(srcImagePath,imageX,imageY,imageW,imageH);
//            }
        }
        return "user/uploadImg/test";
    }
}
